package com.weng.cloud.service8881.concurrenct.algorithm.recursion.resolver;

import com.weng.cloud.service8881.concurrenct.algorithm.recursion.base.Node;
import com.weng.cloud.service8881.concurrenct.algorithm.recursion.base.Puzzle;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: weng
 * @Date: 2022/7/9
 * @Description: com.weng.cloud.service8881.concurrenct.algorithm.recursion.resolver
 */

public class EnhancedConcurrentPuzzleSolver<P, M> extends ConcurrentPuzzleSolver<P, M> {

    private final AtomicInteger taskCount = new AtomicInteger(0);

    public EnhancedConcurrentPuzzleSolver(ExecutorService executorService, ConcurrentMap<P, Boolean> seen) {
        super( executorService, seen);
    }

    protected Runnable newTask(P position, M move, Node<P, M> node) {
        return null;
    }

    class CountingSolverTask extends SolverTask {

        public CountingSolverTask(P position, M move, Node<P, M> prev) {
            super(position, move, prev);
        }

        public void run() {
            try {
                super.run();
            } finally {
                if (taskCount.decrementAndGet() == 0) {
                    solution.setValue(null);
                }
            }
        }
    }
}
